<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pica extends MX_Controller {
  function __construct() {
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('mobile_detect');
    $this->load->library('nbscache');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->model('outlet/moutlet');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();
    
    
  }
	
  function index($id_actifity_report){
    $sdm = $this->global_models->get("d_pica_new", array("id_actifity_report" => $id_actifity_report, "kategori" => 1));
    $service_part = $this->global_models->get("d_pica_new", array("id_actifity_report" => $id_actifity_report, "kategori" => 2));
    $admin = $this->global_models->get("d_pica_new", array("id_actifity_report" => $id_actifity_report, "kategori" => 3));
    $promo = $this->global_models->get("d_pica_new", array("id_actifity_report" => $id_actifity_report, "kategori" => 4));
    $tools = $this->global_models->get("d_pica_new", array("id_actifity_report" => $id_actifity_report, "kategori" => 5));
    
    $this->template->build('main', 
      array('message' => $pesan,
            'url'     => base_url()."themes/srabon/",
            'id_actifity_report'    => $id_actifity_report,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'foot'    => "",
            'data'    => array("sdm" => $sdm, "service_part" => $service_part, "admin" => $admin, "promo" => $promo, "tools" => $tools),
          ));
    $this->template
      ->set_layout('default')
      ->build('main');
  }
	
  function add_new($id_actifity_report, $kategori, $id_pica_new = 0){
    $detail = $this->global_models->get("d_pica_new", array("id_pica_new" => $id_pica_new));
    if($this->input->post()){
      $pst = $this->input->post();
//      $this->debug($pst, true);
      if($pst['id_detail']){
        if($pst['fix'] == 2 AND $detail[0]->fix != 2){
          $date_finish = date("Y-m-d");
        }
        $kirim = array(
          "id_actifity_report"          => $id_actifity_report,
          "id_outlet"                   => $this->global_models->get_field("m_actifity_report", "id_outlet", array("id_actifity_report" => $id_actifity_report)),
          "problem"                     => $pst['problem'],
          "corrective"                  => $pst['corrective'],
          "due_date"                    => $pst['due_date'],
          "kategori"                    => $kategori,
          "fix"                         => $pst['fix'],
          "tanggal_buat"                => $pst['tanggal_buat'],
          "tanggal_selesai"             => $date_finish,
          "update_by_users"             => date("Y-m-d H:i:s"),
        );
        $id_pica_new = $this->global_models->update("d_pica_new", array("id_pica_new" => $pst['id_detail']), $kirim);
        
        if(is_array($pst['log_note'])){
          foreach($pst['log_note'] AS $k => $note){
            $kirim_log[] = array(
              "id_pica_new"         => $pst['id_detail'],
              "tanggal"             => $pst['log_date'][$k],
              "note"                => $note,
              "create_by_users"             => date("Y-m-d H:i:s"),
              "create_date"                 => $this->session->userdata("id"),
            );
          }
          $this->global_models->insert_batch("d_pica_new_log", $kirim_log);
        }
        
      }
      else{
        if($pst['fix'] == 2){
          $date_finish = date("Y-m-d");
        }
        $kirim = array(
          "id_actifity_report"          => $id_actifity_report,
          "id_outlet"                   => $this->global_models->get_field("m_actifity_report", "id_outlet", array("id_actifity_report" => $id_actifity_report)),
          "problem"                     => $pst['problem'],
          "corrective"                  => $pst['corrective'],
          "due_date"                    => $pst['due_date'],
          "kategori"                    => $kategori,
          "fix"                         => $pst['fix'],
          "tanggal_buat"                => $pst['tanggal_buat'],
          "tanggal_selesai"             => $date_finish,
          "create_by_users"             => date("Y-m-d H:i:s"),
          "create_date"                 => $this->session->userdata("id"),
        );
        $id_pica_new = $this->global_models->insert("d_pica_new", $kirim);
      }
      if($id_pica_new){
        $this->session->set_flashdata('success', 'Data Tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data Tidak Tersimpan');
      }
      redirect("pica/index/{$id_actifity_report}");
    }
    else{
      $log = $this->global_models->get_query("SELECT *"
        . " FROM d_pica_new_log"
        . " WHERE id_pica_new = {$id_pica_new}"
        . " ORDER BY tanggal DESC");
      $this->template->build('add-new', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'id_actifity_report'    => $id_actifity_report,
              'menu_drop'   => $this->menu,
              'detail'      => $detail,
              'history_log' => $log
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new');
    }
  }
  
  function add_kunjungan_supervisi($id_actifity_report, $id_kunjungan_supervisi = 0){
    if($this->input->post()){
      $pst = $this->input->post();
//      $this->debug($pst, true);
      if($pst['id_detail']){
        $kirim = array(
          "id_actifity_report"        => $id_actifity_report,
          "id_outlet"                 => $this->global_models->get_field("m_actifity_report", "id_outlet", array("id_actifity_report" => $id_actifity_report)),
          "pic"                       => $pst['pic'],
          "problem"                   => $pst['problem'],
          "penyebab_masalah"          => $pst['penyebab_masalah'],
          "saran"          => $pst['saran'],
          "status"          => $pst['status'],
          "due_date"                  => $pst['due_date'],
          "tanggal_buat"              => $pst['tanggal_buat'],
          "update_by_users"           => $this->session->userdata("id"),
        );
        $id_kunjungan_supervisi = $this->global_models->update("d_kunjungan_supervisi", array("id_kunjungan_supervisi" => $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
          "id_actifity_report"        => $id_actifity_report,
          "id_outlet"                 => $this->global_models->get_field("m_actifity_report", "id_outlet", array("id_actifity_report" => $id_actifity_report)),
          "pic"                       => $pst['pic'],
          "problem"                   => $pst['problem'],
          "penyebab_masalah"          => $pst['penyebab_masalah'],
          "saran"          => $pst['saran'],
          "status"          => $pst['status'],
          "due_date"                  => $pst['due_date'],
          "tanggal_buat"              => $pst['tanggal_buat'],
          "create_by_users"           => $this->session->userdata("id"),
          "create_date"               => date("Y-m-d H:i:s")
        );
        $id_kunjungan_supervisi = $this->global_models->insert("d_kunjungan_supervisi", $kirim);
      }
      if($id_pica_new){
        $this->session->set_flashdata('success', 'Data Tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data Tidak Tersimpan');
      }
      redirect("pica/kunjungan-supervisi/{$id_actifity_report}");
    }
    else{
      $detail = $this->global_models->get("d_kunjungan_supervisi", array("id_kunjungan_supervisi" => $id_kunjungan_supervisi));
      $this->template->build('add-kunjungan-supervisi', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'id_actifity_report'    => $id_actifity_report,
              'menu_drop'   => $this->menu,
              'detail'      => $detail,
            ));
      $this->template
        ->set_layout('default')
        ->build('add-kunjungan-supervisi');
    }
  }
  
  function finish_pica($id_actifity_report, $id_pica_new){
    $detail = $this->global_models->get("d_pica_new", array("id_pica_new" => $id_pica_new));
    if($detail[0]->fix != 2){
      $this->global_models->update("d_pica_new", array("id_pica_new" => $id_pica_new), array("fix" => 2, "tanggal_selesai" => date("Y-m-d")));
    }
    redirect("pica/index/".$id_actifity_report);
  }
  
  function kunjungan_supervisi($id_actifity_report){
    $data = $this->global_models->get("d_kunjungan_supervisi", array("id_actifity_report" => $id_actifity_report));
    $id_outlet = $this->global_models->get_field("m_actifity_report", "id_outlet", array("id_actifity_report" => $id_actifity_report));
    $outlet = $this->global_models->get_field("m_outlet", "name", array("id_outlet" => $id_outlet));
    $this->template->build('kunjungan-supervisi', 
      array('message' => $pesan,
            'url'     => base_url()."themes/srabon/",
            'id_actifity_report'    => $id_actifity_report,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'foot'    => "",
            'data'    => $data,
            'name'    => $outlet
          ));
    $this->template
      ->set_layout('default')
      ->build('kunjungan-supervisi');
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */